package com.jinlejia.erp.ui.common;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import androidx.appcompat.app.AlertDialog;
import androidx.recyclerview.widget.LinearLayoutManager;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.freedom.yefeng.yfrecyclerview.YfListInterface;
import com.freedom.yefeng.yfrecyclerview.YfListRecyclerView;
import com.jinlejia.erp.R;
import com.jinlejia.erp.adapter.MultiSheetAdapter;
import com.jinlejia.erp.adapter.SheetAdapter;
import com.jinlejia.erp.common.RecycleViewDivider;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 底部多选择框
 */

public class MultiSheetSelect extends AlertDialog {

    private Context context;
    ArrayList<Map<String,Object>> data;
    String title;
    MultiSheetAdapter adapter;
    private List<String> selectList;

    public MultiSheetSelect(Context c, String t, ArrayList<Map<String,Object>> d,List<String> sl) {
        super(c);
        this.context = c;
        this.title = t;
        this.data = d;
        this.selectList = sl;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Window window = this.getWindow();
        // *** 主要就是在这里实现这种效果的.
        window.setContentView(R.layout.dialog_sheet_select);
        window.setGravity(Gravity.BOTTOM);
        window.setBackgroundDrawable(new ColorDrawable());

        WindowManager m = window.getWindowManager();
        Display d = m.getDefaultDisplay(); // 获取屏幕宽、高用

        window.setLayout(LinearLayout.LayoutParams.MATCH_PARENT, (int) (d.getHeight() * 0.4));
        TextView titleTv = (TextView) window.findViewById(R.id.title);
        titleTv.setText(title);
        YfListRecyclerView recyclerView = (YfListRecyclerView) window.findViewById(R.id.yf_recycler);
        recyclerView.setLayoutManager(new LinearLayoutManager(context));
        recyclerView.addItemDecoration(new RecycleViewDivider(
                context, LinearLayoutManager.VERTICAL, 2, context.getResources().getColor(R.color.colorDivider)));
        recyclerView.setHasFixedSize(true);
        adapter = new MultiSheetAdapter(data,selectList);
        recyclerView.setAdapter(adapter);
        window.findViewById(R.id.confirm).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
                if( null != selectListener ){
                    selectListener.select(adapter.getNameString(),adapter.getValueString());
                }
            }
        });

    }


    public interface OnSelectListener {
        public void select(String name, String value);
    }

    private OnSelectListener selectListener;

    public void setOnSelectListener(OnSelectListener listener) {
        this.selectListener = listener;
    }
}
